package basic_practice_1016;

import java.util.Scanner;
/**
 * @author MemoryC
 * */
public class Main {

	public static void main(String[] args) {

		Scanner scanner=new Scanner(System.in);
		
		while(scanner.hasNext()) {
			int n=scanner.nextInt();
			int r=scanner.nextInt();
            int num=0;
            do {
            	if(n%r==1) {
            		num++;
            	}
            	n/=r;
            }while(n>=r);
            if(n==1) {
            	num++;
            }

            System.out.println(num);
		}
		scanner.close();
    }
}